//
//  LocalChoiceEndpointVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/6/25.
//

#import "LocalChoiceEndpointVC.h"
#import "RHWindowTipManage.h"
#import "UIView+Extension.h"
#import "RHLocalEndpointModel.h"
#import "RHLocalChoiceEndpointCell.h"
#import "SearchBarView.h"

@interface LocalChoiceEndpointVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) SearchBarView *searhBar;

@property (nonatomic ,strong) UITextField *textField;

@property (nonatomic ,strong) UIView *bgView;

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *searchList;

@property (nonatomic ,strong) NSArray *dataArray;//tableview数据源
@end

@implementation LocalChoiceEndpointVC

- (NSMutableArray *)searchList{
    if(!_searchList){
        _searchList = [NSMutableArray array];
    }
    return _searchList;
}

- (NSArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI{
    
    [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    
    UIView *touchView = [[UIView alloc] init];
    [touchView setBackgroundColor:UIColor.clearColor];
    [self.view addSubview:touchView];
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kUI_WINDOW_H, kUI_WINDOW_W, (kUI_WINDOW_H*0.7+kTabbarSafeBottomMargin))];
    [_bgView setBackgroundColor:UIColor.whiteColor];
    [self.view addSubview:_bgView];
    
    QMUILabel *lab = [[QMUILabel alloc] init];
    [lab setText:@"请选择端点SN".rh_localized];
    [lab setTextAlignment:NSTextAlignmentLeft];
    [lab setFont:[UIFont boldSystemFontOfSize:16]];
    [lab setTextColor:FontColor(@"#333333")];
    [_bgView addSubview:lab];
    
    _searhBar = [[SearchBarView alloc] init];
    _searhBar.searchField.backgroundColor = CFTabViewBgColor;
    _searhBar.searchField.layer.cornerRadius = 10;
    _searhBar.searchField.layer.masksToBounds = YES;
    [_bgView addSubview:_searhBar];
    
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:FontColor(@"#eeeeee")];
    [_bgView addSubview:lineV];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_tableView setSeparatorColor:FontColor(@"#F5F5F5")];
    [_tableView setBackgroundColor:UIColor.whiteColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHLocalChoiceEndpointCell class] forCellReuseIdentifier:@"RHLocalChoiceEndpointCell"];
    [_bgView addSubview:_tableView];
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W,kTabbarSafeBottomMargin+10)];
    [_tableView setTableFooterView:footView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    touchView.userInteractionEnabled = YES;
    [touchView addGestureRecognizer:tap];
    
    [touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(17);
        make.height.mas_equalTo(49);
        make.right.mas_offset(-17);
        make.top.mas_offset(0);
    }];
    
    [_searhBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(7);
        make.height.mas_equalTo(35);
    }];
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.mas_equalTo(1);
        make.top.equalTo(lab.mas_bottom);
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(50, 0,0, 0));
    }];
    
    [_bgView round:10 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    
    kWeakSelf;
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        if (weakSelf.choiceResultBlock) {
            weakSelf.choiceResultBlock(nil);
        }
        [weakSelf hidden];
    }];
    
    _searhBar.searchBlock = ^(NSString * _Nonnull keyword) {
        [weakSelf searchAction:keyword];
    };
    
    
    self.tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"nodata_icon_1" titleStr:@"No data" detailStr:@""];
}

- (void)setEndPointArr:(NSArray<RHLocalEndpointModel *> *)endPointArr{
    _endPointArr = endPointArr;
    _dataArray = endPointArr;
    [self.tableView reloadData];
}

- (void)setCurModel:(RHLocalEndpointModel *)curModel{
    _curModel = curModel;
    if (self.endPointArr.count == 0) {
        self.endPointArr = @[_curModel];
    }
    [self.tableView reloadData];
}

- (void)hidden{
   
    [UIView animateWithDuration:0.5 animations:^{
        [self->_bgView setFrame:CGRectMake(0,kUI_WINDOW_H, kUI_WINDOW_W, kUI_WINDOW_H*0.75+kTabbarSafeBottomMargin)];
        [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
        
    } completion:^(BOOL finished) {
        [self->_searhBar reset];
        self->_dataArray = self.endPointArr;
        [self.tableView reloadData];
        [[RHWindowTipManage shareWindowTipManage] hidden];
        
    }];
}


- (void)showAnimation{
    [[PrivateMange share].currentUser userName];
    [UIView animateWithDuration:0.5 animations:^{
        [self->_bgView setFrame:CGRectMake(0,kUI_WINDOW_H-(kUI_WINDOW_H*0.7+kTabbarSafeBottomMargin), kUI_WINDOW_W, kUI_WINDOW_H*0.7+kTabbarSafeBottomMargin)];
        [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0.3]];
    }];
}



- (void)hiddenAnimation{
    
    
}

- (void)searchAction:(NSString *)tex{
    if (self.searchList.count > 0) {
        [self.searchList removeAllObjects];
    }

    //([model.sn.lowercaseString rangeOfString:tex.lowercaseString].location != NSNotFound)
    for (RHLocalEndpointModel *model in self.endPointArr) {
        if ([model.sn.lowercaseString containsString:tex.lowercaseString]) {
            [self.searchList addObject:model];
        }
    }
    
    if(tex.length>0){
        self.dataArray = self.searchList;
    }else{
        self.dataArray = self.endPointArr;
    }
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray?_dataArray.count:0;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 45;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RHLocalChoiceEndpointCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHLocalChoiceEndpointCell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    if (self.endPointArr.count>indexPath.row) {
        RHLocalEndpointModel *model = self.dataArray[indexPath.row];
        cell.titleLab.text = [NSString stringWithFormat:@"%@%ld",ValidNullData(self.endpointName),indexPath.row+1];
        [cell setEndPointModel:model];
        if(self.curModel){
            [cell setIsSelect:[self.curModel.sn isEqualToString:model.sn]];
        }else{
            [cell setIsSelect:NO];
        }
        
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RHLocalEndpointModel *model = self.dataArray[indexPath.row];
    if (self.choiceResultBlock) {
        self.choiceResultBlock(model);
    }
    self.curModel = model;
   // [tableView reloadData];
    [self hidden];
}

-(void)dealloc{
    
}

@end
